
import 'package:aichat_flutter_wallet/module/module_browser/provider/browser_proviser.dart';
import 'package:aichat_flutter_wallet/module/module_browser/viewmodel/before_browser.dart';
import 'package:aichat_flutter_wallet/module/module_browser/viewmodel/we3_method.dart';
import 'package:aichat_flutter_wallet/module/module_browser/widget/common_widget.dart';
import 'package:aichat_flutter_wallet/module_common/provider/nav_key.dart';
import 'package:aichat_flutter_wallet/module_common/provider/token_list_provider.dart';
import 'package:aichat_flutter_wallet/module_common/provider/wallet_info_provider.dart';
import 'package:aichat_flutter_wallet/module_common/utils/colors.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/button/bottom_button.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/dialog/bottom_sheet_widget.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/public_tip/public_tip.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_database/lib_database.dart';
import 'package:lib_util/lib_util.dart';

class DappInvokeContractWidget extends StatefulWidget {
  Map<String, dynamic> transationInfo;
  DappInvokeContractWidget({super.key, required this.transationInfo});

  @override
  State<DappInvokeContractWidget> createState() => _DappInvokeContractWidgetState();
}

class _DappInvokeContractWidgetState extends State<DappInvokeContractWidget> {
  String address = NavKey.getContext().read<WalletInfoNotifier>().walletAddress;
  List<Token> list = NavKey.getContext().read<TokenListNotifier>().tokenList;
  String walletName = NavKey.getContext().read<WalletInfoNotifier>().walletName;
  WebSiteInfoItem? webSiteInfoItem = NavKey.getContext().read<BrowserProvider>().webSiteInfoItem;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // 查询主币
    Token token = list.firstWhere((element) {
      return element.contractType == 'native';
    });
    String balance = double.parse(token.balance ?? '0.00').toStringAsFixed(2);
    String value = '';
    if (balance == '0.00') {
      value = '(\$0.00)';
    }
    String balanceValue = "${balance}${token.symbol}${value}";
    // double gasPrice =
    //     double.parse(widget.transationInfo['value']) / pow(10, token.decimals);
    String gasPriceText = "${0.00}${token.symbol}${value}";
    return BottomSheetWidget(
      expand: false,
      title: 'Invoke Contract',
      child: Padding(
        padding: EdgeInsets.only(left: 20.w, right: 20.w, bottom: 20.w),
        child: Column(
          children: [
            CommonWidgetUtil.websizeWidget(
                image: webSiteInfoItem == null ? '' : webSiteInfoItem!.websiteImg,
                title: webSiteInfoItem == null ? "" : webSiteInfoItem!.websiteName,
                subTitle: webSiteInfoItem == null ? "" : webSiteInfoItem!.websiteUrl),
            SizedBox(
              height: 10.w,
            ),
            // Padding(
            //   padding: EdgeInsets.only(
            //     left: 15,
            //   ),
            //   child: CommonWidgetUtil.walletCoinInfoWidget(
            //       image: token.logo ?? '',
            //       coinName: token.symbol,
            //       walletName: walletName,
            //       value: balanceValue),
            // ),
            SizedBox(
              height: 10.w,
            ),
            CommonWidgetUtil.textWidget(
                'Attention! Granting this permission will give the contract access to transfer your %${token.symbol}% If this contract is malicious, there is a risk of theft. Please ensure that the current connection is trustworthy!',
                color: AppColors.c_868391,
                fontsize: 12.sp),
            SizedBox(
              height: 10.w,
            ),
            CommonWidgetUtil.contentItemWidget(
                'From', Text(Web3MethodCall.thumAddress(address: widget.transationInfo['from']))),
            SizedBox(
              height: 10.w,
            ),
            CommonWidgetUtil.contentItemWidget(
                'To', Text(Web3MethodCall.thumAddress(address: widget.transationInfo['to']))),
            SizedBox(
              height: 10.w,
            ),
            CommonWidgetUtil.contentItemWidget('Contract Address', Text(Web3MethodCall.thumAddress(address: address))),
            SizedBox(
              height: 10.w,
            ),
            CommonWidgetUtil.contentItemWidget('Gas Fee', Text(gasPriceText)),
            SizedBox(
              height: 10.w,
            ),
            CommonWidgetUtil.contentItemWidget('Contract Method', Text('Commit')),
            SizedBox(
              height: 20.w,
            ),
            if (widget.transationInfo["data"] != null && widget.transationInfo["data"] != '')
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 15.w),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    CommonWidgetUtil.textWidget('data', color: AppColors.c_333333, fontsize: 12.sp),
                    Text(
                      'xxxxxxxxnfdjaknvjneafriueo5420mlkg3402jlknvo2jvni43jllvni3o4n4nk3njk5t4n54lkw5n4jviwuhjreiq4o3jyi5jwntrkjngj5yr55555555555i3o0i490u530yh4xxxxxxxxnfdjaknvjneafriueo5420mlkg3402jlknvo2jvni43jllvni3o4n4nk3njk5t4n54lkw5n4jviwuhjreiq4o3jyi5jwntrkjngj5yr55555555555i3o0i490u530yh4xxxxxxxxnfdjaknvjneafriueo5420mlkg3402jlknvo2jvni43jllvni3o4n4nk3njk5t4n54lkw5n4jviwuhjreiq4o3jyi5jwntrkjngj5yr55555555555i3o0i490u530yh4',
                      style: TextStyle(color: AppColors.c_333333, fontSize: 12.sp),
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                    )
                  ],
                ),
              ),
            SizedBox(
              height: 20.w,
            ),
            BottomBtn(
              textList: ['Cancel', 'Confirm'],
              onCancel: () {
                Navigator.of(context).pop();
              },
              onConfirm: () {
                Web3MethodCall.checkPassword().then((value) {
                  if (!value) {
                    Toast.show(context, status: '2', message: 'password error, please try again!');
                  }
                });
              },
            )
          ],
        ),
      ),
    );
  }
}
